/* Copyright Andy Mitchell 2007. No warranties */

function AsyncPopuper(gtdinboxPage, elBody){
	this._eventObservers = new EventObservers(this);
	this._gtdinboxPage = gtdinboxPage;
	this._doc = gtdinboxPage.doc;
	
	this._eventObservers.observe( elBody, "mousedown", this.mouseDown, true );
	//this._eventObservers.observe( elBody, "mouseup", this.mouseClick, true );
	this._eventObservers.observe( elBody, "click", this.mouseClick, true );
	
};

AsyncPopuper.prototype.createPopup = function(url, popupType){
	/*
	var asyncPopup = new AsyncPopupUI(this._gtdinboxPage, this._doc.body);
	asyncPopup.popup(popupType);
	asyncPopup.load(url);
	*/
	
	if( url.indexOf("?")>-1 ){
		url = url.replace("?", "?gtd=1&");
	}else{
		url += "?gtd=1";
	}
	
	this._timePopup = (new Date()).valueOf();
	
	if( this._gtdinboxPage.gtdinboxBrowser.settings.async.popupNewTab ){
		var tab = gBrowser.addTab(url);
		gBrowser.moveTabTo(tab, gBrowser.tabContainer.selectedIndex + 1);
		//gBrowser.selectedTab = tab;
	}else{
		var popup = window.open(url, "GTDInboxAsync", "menubar=no,location=no,toolbar=no,width=640,height=480,resizable=yes");
		if( !popup ){	// Blocked
			alert("Crikey! It seems a popup blocker may be preventing GTDInbox from loading this new window.\nPlease disable popup blocking for Gmail and try again.");	// TODO Language
		}
	}
};

AsyncPopuper.prototype.mouseDown = function(event){
try{
	if( !event.shiftKey ) return;
	
	
	var el = GIPROTOTYPE.element(event);
	var url = this.getUrl(el);
	
	
	if( url ){
		GIPROTOTYPE.stop(event);
		this.createPopup(url, el);
	}
}catch(e){
	if( GTDDEBUGMODE ) alert("asyncpopuper mousedown\n" + e.toString());
}
};

AsyncPopuper.prototype.mouseClick = function(event){
	if( this.getUrl( GIPROTOTYPE.element(event) ) ){
		var timeSinceLastPopup = (new Date()).valueOf();
		if( this._timePopup ) timeSinceLastPopup = timeSinceLastPopup - this._timePopup;
		if( event.shiftKey || timeSinceLastPopup<1000 ){
			GIPROTOTYPE.stop(event);
		}
	}
	
}

AsyncPopuper.prototype.getUrl = function(el){

	var url = "";
	if( el.parentNode.className=="tYBwhe" && el.parentNode.getElementsByTagName("BUTTON")[0]==el ){	// Search Button
		var searchStr = el.parentNode.getElementsByTagName("INPUT")[0].value;
		url = "http://mail.google.com/mail/?#search/" + encodeURIComponent(searchStr);
	}else if( el.className=="zD5BAe" && el.nodeName=="SPAN" && el.parentNode.className=="NIPhib" ){	// Compose Button
		url = "http://mail.google.com/mail/?#compose";
	}else if( el.getAttribute("gtdSearch") || el.getAttribute("gtdParentSearch") ){	// Label
		var searchStr = (el.getAttribute("gtdParentSearch")||"") + " " + (el.getAttribute("gtdSearch")||"");
		url = "http://mail.google.com/mail/?#search/" + encodeURIComponent(searchStr);
	}else if( el.getAttribute("gtdLabelName") || el.getAttribute("gtdParentSearch") ){	// Label
		var searchStr = (el.getAttribute("gtdParentSearch")||"") + " " + GIPROTOTYPE.searchableLabel(el.getAttribute("gtdLabelName"));
		url = "http://mail.google.com/mail/?#search/" + encodeURIComponent(searchStr);
	}else if( el.className=="filter-search" ){		// Filter Search Button
		var searchStr = el.parentNode.getElementsByTagName("INPUT")[0].value + " " + (el.getAttribute("gtdParentSearchForSearch")||"");
		url = "http://mail.google.com/mail/?#search/" + encodeURIComponent(searchStr);
	};
	return url;
};

AsyncPopuper.prototype.destroy = function(){
	this._eventObservers.destroy();
};